package com.xteam.youin_supplier.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BeddingItem;
import com.xteam.youin_supplier.model.BeddingListItem;
import com.xteam.youin_supplier.model.BeddingResultItem;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

/**
 * 竞标结果
 */
@EFragment(R.layout.fragment_tender_detail)
public class TenderResultFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.join_bedding)
    ImageButton joinBtn;

    @FragmentArg("competitive_id")
    int competitive_id;

    @ViewById(R.id.tender_addr_content)
    TextView addr;
    @ViewById(R.id.tender_datetime_content)
    TextView datetime;
    @ViewById(R.id.tender_num_content)
    TextView num;
    @ViewById(R.id.tender_unit_content)
    TextView unit;
    @ViewById(R.id.tender_information_content)
    TextView information;


    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        joinBtn.setVisibility(View.GONE);
        topTitle.setText(getResources().getString(R.string.bidding_detail_title));
        loadData();
    }

    private void loadData() {
        requestParams = "competitive_id=" + competitive_id;
        requestUrl = RequestAddr.biddingResultDetail + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>()

                {
                    @Override
                    public void onSuccess(Object o) {
                        super.onSuccess(o);
                        dealData(o);
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        super.onFailure(t, errorNo, strMsg);
                    }
                }

        );
    }

    BeddingResultItem beddingResultItem;

    private void dealData(Object object) {
        Gson gson = new Gson();
        try {
            beddingResultItem = gson.fromJson(object.toString(), BeddingResultItem.class);
        } catch (JsonSyntaxException e) {
            Toast.makeText(getActivity(), R.string.info_empty, Toast.LENGTH_SHORT).show();
            return;
        }
        addr.setText(beddingResultItem.getData().getLocation());
        datetime.setText(beddingResultItem.getData().getEnd_time());
        num.setText(beddingResultItem.getData().getQty());
        unit.setText(beddingResultItem.getData().getName());
        information.setText(beddingResultItem.getData().getCompetitive_note());
    }

    @Click({R.id.back_btn, R.id.join_bedding})
    @Override
    void Click(View view) {
        switch (view.getId()){
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStackImmediate();
                break;
            case R.id.join_bedding:
                MainActivity.getInstance().switchFragment(this, new JoinBeddingFragment_());
                break;
        }
    }
}
